package br.com.klis.batendoumabola.client.view;

import br.com.klis.batendoumabola.client.presenter.UploadPresenter;
import br.com.klis.batendoumabola.client.service.UserImageServiceAsync;
import br.com.klis.batendoumabola.shared.Imagem;
import br.com.klis.batendoumabola.shared.LoginInfo;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.MouseOutEvent;
import com.google.gwt.event.dom.client.MouseOutHandler;
import com.google.gwt.event.dom.client.MouseOverEvent;
import com.google.gwt.event.dom.client.MouseOverHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DecoratedPopupPanel;
import com.google.gwt.user.client.ui.FileUpload;
import com.google.gwt.user.client.ui.FormPanel;
import com.google.gwt.user.client.ui.FormPanel.SubmitCompleteEvent;
import com.google.gwt.user.client.ui.FormPanel.SubmitEvent;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.PopupPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

public class UploadView extends Composite implements UploadPresenter.Display{
	private FormPanel form = new FormPanel();
	private Button submit = new Button("Submit");
	//private UserImageServiceAsync rpc; 
	private VerticalPanel galeria = new VerticalPanel();
	
	
	public FormPanel getForm() {
		return form;
	}

	public Button getSubmit() {
		return submit;
	}

	public UploadView(UserImageServiceAsync rpc1, LoginInfo loginInfo) {
		GWT.log("init PeladasView...");
		//rpc = GWT.create(UserImageService.class);	
		// Create a panel to hold all of the form widgets.
	    VerticalPanel panel = new VerticalPanel();
	    

	    // Create a FileUpload widget.
	    FileUpload upload = new FileUpload();
	    upload.setName("uploadFormElement");
	    upload.setTitle("teste");
	    panel.add(upload);
	    
	    panel.add(submit);
	    
	    //loadGaleria();
	    //panel.add(galeria);
	    
	    form.setWidget(panel);
	    
	    
	 // Because we're going to add a FileUpload widget, we'll need to set the
	    // form to use the POST method, and multipart MIME encoding.
	    form.setEncoding(FormPanel.ENCODING_MULTIPART);
	    form.setMethod(FormPanel.METHOD_POST);
	    //form.setAction(GWT.getModuleBaseURL()+"uploadblob");
	    
	  form.setAction(GWT.getHostPageBaseURL()+"uploadblob");
		//   ok para chamar o servlet form.setAction(GWT.getModuleBaseURL()+"upload");
	    
	    // Add an event handler to the form.
		
	    
		submit.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				GWT.log("Button submit has been pressed.");
				form.submit();
			}
		});
	    
	    form.addSubmitHandler(new FormPanel.SubmitHandler() {
	      public void onSubmit(SubmitEvent event) {
	    	  GWT.log("The form has been submited: "+form.getAction());
	        // This event is fired just before the form is submitted. We can take
	        // this opportunity to perform validation.
	        /*if (tb.getText().length() == 0) {
	          Window.alert("The text box must not be empty");
	          event.cancel();
	        }*/
	      }
	    });
		
		
	    form.addSubmitCompleteHandler(new FormPanel.SubmitCompleteHandler() {
	      public void onSubmitComplete(SubmitCompleteEvent event) {
	    	  GWT.log("The form submit has been completed.");
	        // When the form submission is successfully completed, this event is
	        // fired. Assuming the service returned a response of type text/html,
	        // we can get the result text here (see the FormPanel documentation for
	        // further explanation).
	        Window.alert(event.getResults());
	        
	        texto.setHTML(event.getResults());
	        
	      }
	    });
	    Image img = new Image();
	    img.setUrl("/uploadblob?texto=76");
	    //panel.add(texto);
	    panel.add(img);
	    initWidget(form);
		
	}
	HTML texto = new HTML();
	
	@Override
	public Widget asWidget() {
		return this;
	}
	
	/*private void loadGaleria(){
		rpc.getImagens(new AsyncCallback<ArrayList<Imagem>>() {
	    	public void onSuccess(ArrayList<Imagem> imagens) {
	    		for(Imagem img:imagens){
	    			galeria.add(new Label(img.getFileName()));
	    			GWT.log("Imagem URL: "+img.getImageUrl());
	    			Image image = new Image();
	    			image.setUrl(img.getImageUrl());
	    			galeria.add(image);
	    			HTML imgHtml = new HTML(img.getImageUrl());
	    			galeria.add(imgHtml);
	    			galeria.add(createImageWidget(img));
	    		}
	    	}
	    	public void onFailure(Throwable caught) {
	    		Window.alert("Something went wrong with the rpc getImagens call.");
	    	}
	    }); 
	}*/
	
	private Image createImageWidget(final Imagem imagem) {
		GWT.log("Imagem URL: "+imagem.getImageUrl());
		final Image imageWidget = new Image();
		imageWidget.setUrl(imagem.getImageUrl());
		HTML imgHtml = new HTML(imagem.getImageUrl());
		
		//final Image imageWidget = new Image();
		//imageWidget.setUrl(image.getServingUrl() + "=s200");
		final DecoratedPopupPanel simplePopup = new DecoratedPopupPanel(true);

		imageWidget.addMouseOverHandler(new MouseOverHandler() {

			@Override
			public void onMouseOver(MouseOverEvent event) {
				Widget source = (Widget) event.getSource();
				int left = source.getAbsoluteLeft() + 10;
				int top = source.getAbsoluteTop() + source.getOffsetHeight()
						+ 10;

				simplePopup.setWidth("150px");
				simplePopup.setWidget(new HTML("Uploaded: "
						+ imagem.getCreation()));
				simplePopup.show();
				simplePopup.setPopupPosition(left, top);
			}
		});

		imageWidget.addMouseOutHandler(new MouseOutHandler() {

			@Override
			public void onMouseOut(MouseOutEvent event) {
				simplePopup.hide();
			}
		});

		imageWidget.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				//ImageOverlay imageOverlay = new ImageOverlay(image, parent.getLoginInfo());
				//imageOverlay.addGalleryUpdatedEventHandler(PhotoGallery.this);
				
				final PopupPanel imagePopup = new PopupPanel(true);
				imagePopup.setAnimationEnabled(true);
				//imagePopup.setWidget(imageOverlay);
				imagePopup.setWidget(imageWidget);
				imagePopup.setGlassEnabled(true);
				imagePopup.setAutoHideEnabled(true);

				imagePopup.center();
			}
		});

		return imageWidget;
	}

}
